import requests
import json
from tqdm import tqdm
from pathlib import Path


def exact_funcs(content: str) -> list[str]:
    # 构建请求数据
    data = [{"role": "user", "content": "提取功能点:\n" + content}]

    # 发送POST请求
    response = requests.post(
        url="http://home:5000/chat",
        headers={"Content-Type": "application/json"},
        data=json.dumps(data),
    )

    result = []

    # 检查响应状态
    if response.status_code == 200:
        result = response.json()  # 获取JSON格式的响应数据
    else:
        print(f"请求失败，状态码: {response.status_code}")
        print(f"错误信息: {response.text}")

    return result


# 读取json文件
funcs = []
content = open("/home/x/codes/llm/swift/exact_functions/funcs.json").read()
docs = json.loads(content)
for doc in tqdm(docs):
    result = exact_funcs(doc["desc"])
    tqdm.write(doc["desc"])
    tqdm.write(str(result))
    tqdm.write("")

    # 提取结果
    for res in result:
        if res != "false":
            funcs.append({"id": res, "content": doc["desc"], "desc": res})

print(len(funcs))
Path("/home/x/codes/llm/swift/exact_functions/vector.json").write_text(
    json.dumps(funcs, ensure_ascii=False)
)
